package com.afd.ozblip.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.afd.ozblip.entity.CategoryEntity;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.view.RangeSeekBar;
import com.afd.ozblip.view.RangeSeekBar.OnRangeSeekBarChangeListener;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SearchAllActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private Button searchAllBtn;
	// private LinearLayout priceRangeLayout;
	private LinearLayout areaLayout;
	private LinearLayout categoryLayout;
	private LinearLayout sortLayout;
	private TextView areaTv;
	private TextView categoryTv;
	private TextView sortTv;

	private RadioGroup categoryFilterLeftLayout;
	private FrameLayout categoryFilterRightLayout;
	/****************** 数据变量 **************************/
	private List<HashMap<String, Object>> sortList = null;
	private List<HashMap<String, Object>> dialogAreaList = null;

	// 总分类长度
	private int classLen;
	private int parentClassIndex;
	private List<CategoryEntity> categoryList;
	// 父级类型名称
	private String parentClassName;
	// 子级类型名称
	private String childClassName;
	// 价格起始值
	private Integer beginPrice;
	// 价格终止值
	private Integer endPrice;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_all);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.searchAllBtn = (Button) findViewById(R.id.btn_search_all);
		// create RangeSeekBar as Integer range between 20 and 75
		RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20,
				Integer.parseInt(maxPrice), this);
		// seekBar.setNotifyWhileDragging(true);
		seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
			@Override
			public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar,
					Integer minValue, Integer maxValue) {
				// handle changed range values
				beginPrice = minValue;
				endPrice = maxValue;
			}
		});

		// add RangeSeekBar to pre-defined layout
		ViewGroup layout = (ViewGroup) findViewById(R.id.price_range);
		layout.addView(seekBar);

		this.areaLayout = (LinearLayout) findViewById(R.id.ll_search_all_area);
		this.categoryLayout = (LinearLayout) findViewById(R.id.ll_search_all_category);
		this.sortLayout = (LinearLayout) findViewById(R.id.ll_search_all_sort);

		this.areaTv = (TextView) findViewById(R.id.tv_search_all_area);
		this.categoryTv = (TextView) findViewById(R.id.tv_search_all_category);
		this.sortTv = (TextView) findViewById(R.id.tv_search_all_sort);

		sortList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> smap1 = new HashMap<String, Object>();
		smap1.put("key", "0");
		smap1.put("value", "默认排序");
		sortList.add(smap1);
		HashMap<String, Object> smap2 = new HashMap<String, Object>();
		smap2.put("key", "1");
		smap2.put("value", "人气排序");
		sortList.add(smap2);
		HashMap<String, Object> smap3 = new HashMap<String, Object>();
		smap3.put("key", "2");
		smap3.put("value", "总体评价排序");
		sortList.add(smap3);
		HashMap<String, Object> smap4 = new HashMap<String, Object>();
		smap4.put("key", "3");
		smap4.put("value", "距离排序");
		sortList.add(smap4);
		HashMap<String, Object> smap5 = new HashMap<String, Object>();
		smap5.put("key", "4");
		smap5.put("value", "最新排序");
		sortList.add(smap5);

	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener allClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.btn_search_all:
					Intent cityBusinessIntent = new Intent(
							SearchAllActivity.this, CityBusinessActivity.class);
					if (areaTv.getTag() != null) {
						cityBusinessIntent.putExtra("areaId", areaTv.getTag()
								.toString());
					}
					cityBusinessIntent.putExtra("parClass", parentClassName);
					cityBusinessIntent.putExtra("chiClass", childClassName);
					cityBusinessIntent.putExtra("beginPrice", beginPrice);
					cityBusinessIntent.putExtra("endPrice", endPrice);
					startActivity(cityBusinessIntent);
					break;
				case R.id.ll_search_all_area:
					showAreaDialog(areaList, areaTv);
					break;
				case R.id.ll_search_all_category:
					showCategoryDialog(categoryTv);
					break;
				case R.id.ll_search_all_sort:
					showListDialog(sortList, sortTv);
					break;
				default:
					break;
				}
			}
		};
		this.searchAllBtn.setOnClickListener(allClick);
		this.areaLayout.setOnClickListener(allClick);
		this.categoryLayout.setOnClickListener(allClick);
		this.sortLayout.setOnClickListener(allClick);
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	private Dialog dialog;

	public void showCategoryDialog(final TextView resultTv) {
		View view = LayoutInflater.from(this).inflate(R.layout.category, null);
		dialog = new Dialog(SearchAllActivity.this, R.style.listDialog);
		initCategory(view, resultTv);
		dialog.setContentView(view); // 设置显示用户信息列表的对话框视图
		dialog.show();
	}

	// TODO 初始化分类
	private void initCategory(View v, final TextView resultTv) {
		this.categoryFilterLeftLayout = (RadioGroup) v
				.findViewById(R.id.ll_conditon_filter_category_left);
		this.categoryFilterRightLayout = (FrameLayout) v
				.findViewById(R.id.ll_conditon_filter_category_right);

		LayoutInflater inflater = LayoutInflater.from(this);
		categoryList = Constants.getCategoryList();
		classLen = categoryList.size();
		View.OnClickListener leftClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				parentClassIndex = index;
				for (int i = 0, len = classLen; i < len; i++) {
					categoryFilterRightLayout.getChildAt(i).setVisibility(8);
				}
				categoryFilterRightLayout.getChildAt(index).setVisibility(0);
			}
		};
		View.OnClickListener rightClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				parentClassName = categoryList.get(parentClassIndex)
						.getParentClassName();
				if ("热门分类".equals(parentClassName)) {
					parentClassName = "";
				}
				childClassName = categoryList.get(parentClassIndex)
						.getChildClassList().get(index);
				resultTv.setText(parentClassName + "/" + childClassName);
				dialog.dismiss();
			}
		};
		for (int i = 0, len = categoryList.size(); i < len; i++) {
			View leftView = inflater.inflate(R.layout.category_left_item, null);
			RadioButton leftRB = (RadioButton) leftView;
			String parentCN = categoryList.get(i).getParentClassName();
			leftRB.setText(parentCN);
			leftRB.setId(i);
			if ("热门分类".equals(parentCN)) {
				leftRB.setChecked(true);
			}
			// 设置drawableleft
			BitmapDrawable leftbd = getListIco(i);
			leftRB.setCompoundDrawablesWithIntrinsicBounds(leftbd, null, null,
					null);
			// 设置tag
			leftRB.setTag(i);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			leftView.setOnClickListener(leftClick);
			this.categoryFilterLeftLayout.addView(leftView, lp);
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT));
			ll.setOrientation(LinearLayout.VERTICAL);
			ll.setVisibility(i == 0 ? 0 : 8);
			List<String> childClassList = categoryList.get(i)
					.getChildClassList();
			for (int j = 0, childLen = childClassList.size(); j < childLen; j++) {
				View rightView = inflater.inflate(R.layout.category_right_item,
						null);
				rightView.setBackgroundResource(R.drawable.ll_bg_selector);
				rightView.setTag(j);
				rightView.setOnClickListener(rightClick);
				TextView rightTv = (TextView) rightView
						.findViewById(R.id.tv_category_right);
				rightTv.setText(childClassList.get(j));

				ll.addView(rightView, lp);
			}
			this.categoryFilterRightLayout.addView(ll);
		}
	}

	private BitmapDrawable getListIco(int index) {
		Resources res = getResources();
		BitmapDrawable bd = null;
		Bitmap bm = null;
		switch (index) {
		case 0:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico1);
			bd = new BitmapDrawable(res, bm);
			break;
		case 1:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico2);
			bd = new BitmapDrawable(res, bm);
			break;
		case 2:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico3);
			bd = new BitmapDrawable(res, bm);
			break;
		case 3:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico4);
			bd = new BitmapDrawable(res, bm);
			break;
		case 4:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico5);
			bd = new BitmapDrawable(res, bm);
			break;
		case 5:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico6);
			bd = new BitmapDrawable(res, bm);
			break;
		case 6:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico7);
			bd = new BitmapDrawable(res, bm);
			break;
		case 7:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico8);
			bd = new BitmapDrawable(res, bm);
			break;

		default:
			break;
		}
		return bd;
	}

}